<?php
//wcf imports
require_once(WCF_DIR.'lib/page/SortablePage.class.php');
require_once(WCF_DIR.'lib/data/user/UserProfile.class.php');
require_once(WCF_DIR.'lib/page/util/menu/PageMenu.class.php');
require_once(WCF_DIR.'lib/data/user/option/UserOptions.class.php');

/**
 * Shows the TeamPage
 *
 * @author	Oliver Kliebisch (Hawkes), Martin Schwendowius (Sani9000), Matthias Z. (Math-Board)
 * @copyright	2009-2010 WBBLite2.de & WoltLab Community
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 */
class TeamPage extends SortablePage {
	public $templateName = 'team';
	public $activeMenuItem = 'wcf.header.menu.memberslist';
	public $teamUsers = array();
	public $userOptions;
	public $defaultSortField = 'username';
	public $defaultSortOrder = 'ASC';
	public $homepageFieldActive = false;

	public function validateSortField() {
		parent::validateSortField();

		switch ($this->sortField) {
			case 'username':
			case 'email' :
			case 'website' : 
			break;
			
			case 'avatar' : 
				$this->sortField = 'avatar.avatarID'; 
			break;
			
			case 'homepage' :
				$this->userOptions = new UserOptions('short');
				$optionsArray = $this->userOptions->getOption('homepage');
				$this->sortField = 'user_option.userOption'.$optionsArray['optionID'];
				$this->homepageFieldActive = true;
			break;
			
			default:
				$this->sortField = $this->defaultSortField;
			break;
		}
	}

	public function readData() {
		parent::readData();
		//read team Data
		$this->readTeam();
	}

	public function assignVariables() {
		parent::assignVariables();
		
		WCF::getTPL()->assign(array(
			'team' => $this->teamUsers,
			'searchID' => false,
			'hasFriends' => $this->hasFriends()
		));
		
	}

	public function show() {
		PageMenu::setActiveMenuItem($this->activeMenuItem);
		parent::show();
	}

	public function readTeam() {
		$sql = "SELECT		user.*, usergroup.groupID, usergroup.groupName, 
		avatar.avatarID, avatar.avatarName, avatar.avatarExtension, avatar.width, avatar.height, avatar.neededPoints, 
		rank.rankID, rank.neededPoints, rank.rankTitle, rank.rankImage, rank.repeatImage,rank.gender, 0 AS buddy, 0 AS ignoredUser,
		session.requestURI, session.requestMethod, session.ipAddress, session.userAgent, user_option.*
 		FROM 		wcf".WCF_N."_group usergroup
 		LEFT JOIN 	wcf".WCF_N."_user_to_groups user_to_groups
		ON		(user_to_groups.groupID = usergroup.groupID)
 		LEFT JOIN 	wcf".WCF_N."_user user
		ON		(user.userID = user_to_groups.userID)
		LEFT JOIN wcf".WCF_N."_avatar avatar
		ON (avatar.avatarID = user.avatarID)
		LEFT JOIN 	wcf".WCF_N."_user_rank rank
		ON		(rank.rankID = user.rankID)
		LEFT JOIN wcf".WCF_N."_user_option_value user_option
		ON (user_option.userID = user.userID)
		LEFT JOIN wcf".WCF_N."_session session
		ON		(session.userID = user.userID AND session.packageID = ".PACKAGE_ID." AND session.lastActivityTime > ".(TIME_NOW - USER_ONLINE_TIMEOUT).")
		WHERE 		usergroup.groupID IN (".TEAM_PAGE_GROUP_IDS.")
		ORDER BY 	usergroup.groupID, ".$this->sortField." ".$this->sortOrder;
		$result = WCF::getDB()->sendQuery($sql);
		while ($row = WCF::getDB()->fetchArray($result)) {
			if (!$row['userID']) continue;
			if (!isset($this->teamUsers[$row['groupID']])) {
				$this->teamUsers[$row['groupID']] = array('members' => array(), 'groupName' => $row['groupName']);
			}
			$this->teamUsers[$row['groupID']]['members'][] = new UserProfile(null, $row);

		}
		if($this->sortField == 'avatar.avatarID') $this->sortField = 'avatar';
		if($this->homepageFieldActive) $this->sortField = 'homepage';
		$this->sortGroups();
	}

	protected function sortGroups() {
		$groupIDs = explode(',', TEAM_PAGE_GROUP_IDS);
		$sortedTeamUsers = array();
		foreach($groupIDs as $groupID) {
			if (!isset($this->teamUsers[$groupID])) continue;
			$sortedTeamUsers[] = $this->teamUsers[$groupID];
		}
		$this->teamUsers = $sortedTeamUsers;
	}
	
	public static function hasFriends() {
		$sql = "SELECT	COUNT(*) AS count
			FROM	wcf".WCF_N."_user_whitelist
			WHERE	userID = ".WCF::getUser()->userID."
				AND confirmed = 1";
		$row = WCF::getDB()->getFirstRow($sql);
		return ($row['count'] > 0);
	}
}
?>